package Arrays;

import java.util.HashSet;
import java.util.Set;

public class _888_FairCandySwap {
    public int[] fairCandySwap(int[] A, int[] B) {
        int[] res = new int[2];
        Set<Integer> rest = new HashSet<>();
        int sumA = 0, sumB = 0;
        for (int a : A) {
            sumA += a;
        }
        for (int b : B) {
            rest.add(b);
            sumB += b;
        }
        for (int a : A) {
            int b = (sumB - sumA + 2 * a) / 2;
            if (rest.contains(b)) {
                res[0] = a;
                res[1] = b;
                return res;
            }
        }

        return res;
    }
}
